package org.bigdata.framework.core.utils;

 import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFHyperlink;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
 
 public class ExcelUtils{
   
	 public static boolean createExcelFile(List<HashMap<String,Object>> list ,String newExcelPath) {  
    	boolean isCreateSuccess = false;  
    	SXSSFWorkbook workbook = new SXSSFWorkbook();
    	try {  
    		workbook = new  SXSSFWorkbook();
            workbook.setCompressTempFiles(true);
    	}catch(Exception e) {  
    		System.out.println("It cause Error on CREATING excel workbook: ");  
    		e.printStackTrace();  
    	}  
    	List<String> head = new ArrayList<String>();
    	if(list.size()>0){
    		Map<String, Object> map = list.get(0);
    		for (Map.Entry<String, Object>entry : map.entrySet()) {
    			head.add(entry.getKey());
			}
    	}
    	if(workbook != null) {  
    		Sheet sheet= workbook.createSheet("Sheet1");
    		System.out.println(sheet.getColumnWidth(0));  
    		for (int rowNum = 0; rowNum < list.size(); rowNum++) {  
    			Row row = sheet.createRow(rowNum);  
    			if(rowNum == 0){
    				for (int i = 0; i < head.size(); i++) {
    					Cell cell = row.createCell(i, Cell.CELL_TYPE_STRING);
    					cell.setCellValue(head.get(i).toString()); 
    				}
    			}else{
    				Map<String,Object>list2 = list.get(rowNum-1);
    				for (int i = 0; i < head.size(); i++) {
    					Cell cell = row.createCell(i, Cell.CELL_TYPE_STRING);
    					cell.setCellValue(list2.get(head.get(i))==null?null:list2.get(head.get(i)).toString()); 
    				}
    			}
    		}  
    		System.out.println("分析数据完成，开始写入文件...");  
    		try {  
    			FileOutputStream outputStream = new FileOutputStream(newExcelPath);  
    			workbook.write(outputStream);  
    			outputStream.flush();  
    			outputStream.close();
    			isCreateSuccess = true;  
    		} catch (Exception e) {  
    			System.out.println("It cause Error on WRITTING excel workbook: ");  
    			e.printStackTrace();  
    		} finally {
                workbook.dispose();
            }
        }
    	File sss = new File(newExcelPath);  
    	System.out.println(sss.getAbsolutePath());  
    	return isCreateSuccess;  
    } 
    public static void main(String[] args) throws Exception {
    	List<HashMap<String,Object>> lm=new ArrayList<HashMap<String,Object>>();
    	for (int i = 0; i < 10; i++) {
    		HashMap<String,Object> m = new HashMap<String, Object>();
    		m.put("a", "1");
    		lm.add(m);
    		
		}
    	createExcelFile(lm, "D:\\Project\\tomcat\\apache-tomcat-7.0.88-8088\\webapps\\jianxue\\log_excel/1579444628050.xlsx");
    	System.out.println("完成");
   }
}